using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;
using BitmapFont = FlatRedBall.Graphics.BitmapFont;
using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;


#endif
using NereusGame.Entities.Weapons;
namespace NereusGame.Entities.Ships.Enemies
{
    public partial class NeutralMiner
    {
        private List<Asteroid> asteroids;
        private float resetDistance;
        private const int MAX_WEAPONS = 1;
        public const float MAX_HEALTH = 75f;      // Initial Health of the player.
        private const float BODY_RADIUS = 2f;
        private void CustomInitialize()
        {
            InitializeMiner();
            InitializeWeApons();

        }

        private void CustomActivity()
        {

        }

        private void CustomDestroy()
        {
            base.Destroy();

        }

        private static void CustomLoadStaticContent(string contentManagerName)
        {


        }


        private void InitializeMiner()
        {

            Tex.Texture = minerTexture;
            Tex.ScaleX = Tex.ScaleY = Body.Radius = BODY_RADIUS;
            CurrentHealth = MaxHealth = MAX_HEALTH;
            HealthBar.RelativePosition = new Vector3(0, Body.Radius + HEALTHBAR_OFFSET, 0);
            PrimaryTargetRange = 50f;
            resetDistance = PrimaryTargetRange * 2.2f;
            IsNeutralMiner = true;
            points = -1000;

        }

        private void InitializeWeApons()
        {
            weaponInventory = new Weapon[MAX_WEAPONS];
            weaponInventory[0] = new Gun(CONTEN_MANAGER);
            weaponInventory[0].UpgradeWeapon();    // set gun's level to 1
            weaponInventory[0].UpgradeWeapon();    // set gun's level to 2
            weaponInventory[weaponSelector].SetFireRate(60);
            weaponSelector = 0;
        }


        protected override void NormalState()
        {
            if (PrimaryTarget == null || !PrimaryTarget.IsAlive())
            {
                state = EnemyState.Angry;
                return;
            }

            if ((Position - PrimaryTarget.Position).Length() <= PrimaryTargetRange)
            {
                Drag = FRICTION * 4.3f;
                Acceleration = Vector3.Zero;
                ShootAsteroid();
            }
            else
            {
                Drag = FRICTION;
                MoveTowardsAsteroid();

            }
        }

        protected override void AngryState()
        {
            Asteroid target = FindClosestAsteroid();
            if (target != null)
            {
                PrimaryTarget = target;
                state = Enemy.EnemyState.Normal;
            }
        }

        private void MoveTowardsAsteroid()
        {
            Vector3 directionToPtimaryTarget = PrimaryTarget.Position - Position;
            directionToPtimaryTarget.Normalize();
            Acceleration = directionToPtimaryTarget * 17f;
        }

        private void ShootAsteroid()
        {
            weaponInventory[weaponSelector].SetFireRate(60);
            Vector3 directionToPtimaryTarget = PrimaryTarget.Position - Position;
            directionToPtimaryTarget.Normalize();

            if (directionToPtimaryTarget.X >= 0)
                RotationZ = -(float)Math.Acos(directionToPtimaryTarget.Y);
            else
                RotationZ = (float)Math.Acos(directionToPtimaryTarget.Y);

            weaponInventory[weaponSelector].Shoot(this, Position, (BODY_RADIUS + 1f), RotationZ, Velocity);
        }

        public void SetAsteroidsList(List<Asteroid> list)
        {
            asteroids = list;
        }

        private Asteroid FindClosestAsteroid()
        {
            Asteroid ClosestAsteroid = null;
            if(asteroids != null && asteroids.Count > 0)
            {
               
                ClosestAsteroid = asteroids[0];
       
                for (int i = 1; i < asteroids.Count; i++)
                {
                    if ((Position - asteroids[i].Position).Length() < (Position - ClosestAsteroid.Position).Length())
                        ClosestAsteroid = asteroids[i];

                }
            }
            return ClosestAsteroid;
        }
    }
}
